﻿using SilverPOP.Data.Process.DAL;
using SilverPOP.Data.Process.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Helpers;

namespace SilverPOP.Data.Process.BLL
{
    public class TaskListBLL : BaseBLL
    {



        public TaskListBLL() 
        {
            DAL.DAL.ConnectionString=ConfigHelpers.SQLConnectionString;
        }


        public List<TaskListEntity> GetAllActiveTask()
        {
            List<ParamWithValue> spParamValue = new List<ParamWithValue>();

            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("spr_GetTaskList", spParamValue);

            if (dataList != null)
            {
                List<TaskListEntity> taskList = new List<TaskListEntity>();

                foreach (DataReaderObject dataReader in dataList)
                {
                    TaskListEntity task = new TaskListEntity();

                    task.TaskListId = (int)dataReader.ColumnValueList[0];
                    task.ProjectId = (int)dataReader.ColumnValueList[1];
                    task.CampaignId = (int)dataReader.ColumnValueList[2];
                    task.ProjectName = dataReader.ColumnValueList[3] != DBNull.Value ? dataReader.ColumnValueList[3].ToString() : String.Empty;
                    task.ServerName = dataReader.ColumnValueList[4] != DBNull.Value ? dataReader.ColumnValueList[4].ToString() : String.Empty;
                    task.DatabaseName = dataReader.ColumnValueList[5] != DBNull.Value ? dataReader.ColumnValueList[5].ToString() : String.Empty;
                    task.Username = dataReader.ColumnValueList[6] != DBNull.Value ? dataReader.ColumnValueList[6].ToString() : String.Empty;
                    task.Password = dataReader.ColumnValueList[7] != DBNull.Value ? dataReader.ColumnValueList[7].ToString() : String.Empty;                
                    task.TaskType = dataReader.ColumnValueList[8] != DBNull.Value ? dataReader.ColumnValueList[8].ToString() : String.Empty;

                    taskList.Add(task);

                }

                return (taskList != null && taskList.Count > 0) ? taskList : null;

            }
            return null;
        }


        public void UpdateTaskTableBasedOnOperationState(int taskListId, int campaignId,bool isProcessCompleted)
        {
            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@TaskListId", taskListId));
            spParamValue.Add(new ParamWithValue("@CampaignId", campaignId));
            spParamValue.Add(new ParamWithValue("@IsProcessCompleted", isProcessCompleted));

            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("spr_UpdateTaskBasedOnOperationProcess", spParamValue);
        }
    }
}
